package com.hrpc;

import com.hrpc.discovery.Register;
import com.hrpc.exceptions.NetworkException;
import com.hrpc.proxy.handler.RpcClientInvocationHandler;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class ReferenceConfig<T> {
    private Class<T> interfaceRef;

    private Register register;

    private ReferenceConfig referenceConfig;
    //分组信息
    private String group;

    public ReferenceConfig() {
    }

    public ReferenceConfig(Class<T> interfaceRef, Register register) {
        this.interfaceRef = interfaceRef;
        this.register = register;
    }

    public Class<T> getInterfaceRef() {
        return interfaceRef;
    }

    public String getGroup() {
        return group;
    }

    public void setInterfaceRef(Class<T> interfaceRef) {
        this.interfaceRef = interfaceRef;
    }

    public Register getRegister() {
        return register;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

    /**
     * 代理设计模式，生成一个api接口的代理对象
     * @return
     */
    public T get() {
        //此处一定是使用了动态代理完成了一些工作
        ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
        Class<T>[] classes=new Class[]{interfaceRef};
        InvocationHandler invocationHandler=new RpcClientInvocationHandler(register,interfaceRef,group);
        //使用动态代理生成代理对象
        Object helloProxy = Proxy.newProxyInstance(classLoader, classes,invocationHandler);
        return (T)helloProxy;
    }

    public void setGroup(String group) {
        this.group=group;
    }
}
